<?php
session_start();
if (is_file('config.php')) die('系统已安装，如需重装请先删除 config.php');
$sqlStructure = file_get_contents('install.sql');

if ($_POST) {
    $cfg = <<<EOT
<?php
\$DB_HOST = '{$_POST['host']}';
\$DB_NAME = '{$_POST['name']}';
\$DB_USER = '{$_POST['user']}';
\$DB_PASS = '{$_POST['pass']}';
\$DB_CHAR = 'utf8mb4';
EOT;
    file_put_contents('config.php', $cfg);
    $pdo = new PDO("mysql:host={$_POST['host']};dbname={$_POST['name']};charset=utf8mb4", $_POST['user'], $_POST['pass']);
    $pdo->exec($sqlStructure);
    $pdo->prepare("INSERT INTO admin(username,password) VALUES(?,?)")
         ->execute([$_POST['admin_user'], password_hash($_POST['admin_pass'], PASSWORD_DEFAULT)]);
    echo '<div class="alert alert-success text-center">安装完成！<a href="login.php" class="btn btn-primary mt-2">进入系统</a></div>';
    exit;
}
?>
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>仓库管理系统 V 0.1 - 安装</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <link rel="stylesheet" href="bootstrap.min.css">
  <style>
    body{background:#f5f5f7}
    .install-card{max-width:420px;margin:60px auto}
    .install-card h4{font-weight:600}
  </style>
</head>
<body>
<div class="container">
  <div class="card shadow install-card">
    <div class="card-body">
      <h4 class="text-center mb-4">仓库管理系统 V 1.0</h4>
      <form method="post">
        <h6 class="text-muted mb-2">数据库连接</h6>
        <input class="form-control mb-2" name="host" value="localhost" required placeholder="主机">
        <input class="form-control mb-2" name="name" required placeholder="数据库名">
        <input class="form-control mb-2" name="user" required placeholder="用户名">
        <input class="form-control mb-3" name="pass" type="password" placeholder="密码">
        <h6 class="text-muted mb-2 mt-3">创建管理员</h6>
        <input class="form-control mb-2" name="admin_user" required placeholder="用户名（支持中文）">
        <input class="form-control mb-3" name="admin_pass" type="password" required placeholder="密码">
        <button class="btn btn-primary w-100">立即安装</button>
      </form>
    </div>
  </div>
</div>
<script src="bootstrap.bundle.min.js"></script>
</body>
</html>